package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_tracker.blood_pressure

import com.bp.hearthealth.pulse.lnapps.bp_db.bp_bloodpressure.BpBloodPressureDao
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_network.BaseRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import java.util.Calendar


class BloodPressureRepository(private val bloodPressureDao: BpBloodPressureDao) : BaseRepository() {

    suspend fun getBloodPressureForDateRange(
        userId: String, startOfDayInMillis: Long, endOfDayInMillis: Long
    ) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                LogUtil.i("查询起始时间：$startOfDayInMillis   结束时间: $endOfDayInMillis")
                bloodPressureDao.getBloodPressureForDateRange(
                    userId, startOfDayInMillis, endOfDayInMillis
                )
            } catch (e: Exception) {
                LogUtil.e(e)
                null
            }
        }
    }

    suspend fun getBloodPressureByUserId(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getBloodPressureByUserId(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getBloodPressureDataByUserId(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getBloodPressureDataByUserId(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getLatestBloodPressureData(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getLatestBloodPressureData(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getAverageValuesForLast24Hours(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                val calendar = Calendar.getInstance()
                calendar.add(Calendar.HOUR_OF_DAY, -24)
                val startTime = calendar.timeInMillis
                bloodPressureDao.getAverageValuesForLast24Hours(userId, startTime)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getAverageValuesForUser(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getAverageValuesForUser(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getMaxValuesForUser(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getMaxValuesForUser(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getMinValuesForUser(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getMinValuesForUser(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getTodayBloodPressureData(userId: String, startOfDay: Long, endOfDay: Long) =
        withContext(Dispatchers.IO) {
            coroutineScope {
                try {
                    bloodPressureDao.getTodayBloodPressureData(userId, startOfDay, endOfDay)
                } catch (e: Exception) {
                    null
                }
            }
        }

    suspend fun getStatusTypesByUserId(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getStateTypesByUserId(userId)
            } catch (e: Exception) {
                null
            }
        }
    }

    suspend fun getLastThreeBloodPressureData(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            try {
                bloodPressureDao.getLastThreeBloodPressureData(userId)
            } catch (e: Exception) {
                null
            }
        }
    }
}